package com.pub.functions;

public class NumberFuncs {
	// 将iSource转为长度为iArrayLen的byte数组，字节数组的低位是整型的低字节位 
    public static byte[] toByteArray(int iSource, int iArrayLen) { 
        byte[] bLocalArr = new byte[iArrayLen]; 
        for ( int i = 0; (i < 4) && (i < iArrayLen); i++) { 
            bLocalArr[i] = (byte)( iSource>>8*i & 0xFF ); 
           
        } 
        return bLocalArr; 
    }    
    
    public static byte[] toByteArray(long iSource, int iArrayLen) { 
    	long temp = iSource; 
        byte[] b = new byte[iArrayLen]; 
        for (int i = 0; i < b.length; i++) { 
            b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位
            temp = temp >> 8; // 向右移8位 
        } 
        return b; 
     
    }    

//     // 将byte数组bRefArr转为一个整数,字节数组的低位是整型的低字节位 
//    public static int toInt(byte[] bRefArr) { 
//        int iOutcome = 0; 
//        byte bLoop; 
//        for ( int i =0; i<bRefArr.length ; i++) { 
//            bLoop = bRefArr[i]; 
//            iOutcome+= (bLoop & 0xFF) << (8 * i); 
//        }
//        return iOutcome; 
//    }  
    public static int toInt(byte[] bRefArr) {
        int iOutcome = 0;
        byte bLoop;

        for (int i = 0; i < bRefArr.length; i++) {
            bLoop = bRefArr[i];
            iOutcome += (bLoop & 0xFF) << (8 * i);
        }
        return iOutcome;
    }
    
    public static long toLong(byte[] b) { 
    	long s = 0; 
        long s0 = b[0] & 0xff;// 最低位 
        long s1 = b[1] & 0xff; 
        long s2 = b[2] & 0xff; 
        long s3 = b[3] & 0xff; 
        long s4 = b[4] & 0xff;// 最低位 
        long s5 = b[5] & 0xff; 
        long s6 = b[6] & 0xff; 
        long s7 = b[7] & 0xff; 
 
        // s0不变 
        s1 <<= 8; 
        s2 <<= 16; 
        s3 <<= 24; 
        s4 <<= 8 * 4; 
        s5 <<= 8 * 5; 
        s6 <<= 8 * 6; 
        s7 <<= 8 * 7; 
        s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7; 
        return s;    
   
    }    

    public static void main(String[] agr){
//    	 byte[] temp={0x00, 0x00 ,0x00 ,0x00 ,0x00 ,0x00,0x01, 0x20 ,0x0B ,0x64 ,0x30,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x31     
//    				,0x56, 0x30 ,0x31 ,0x30 ,0x31 ,0x31, 0x30, 0x31 ,0x31 ,0x30 ,0x36,0x30 ,0x30 ,0x30 ,0x30 ,0x34 ,0x36 ,0x32 ,0x38 ,0x39    
//    				,0x38, 0x36 ,0x30 ,0x31 ,0x31 ,0x34, 0x39, 0x30 ,0x33 ,0x38 ,0x30,0x30 ,0x32 ,0x30 ,0x34 ,0x33 ,0x34 ,0x38 ,0x30 ,0x30    
//    				,0x30, 0x32 ,0x35 ,0x46 ,0x32 ,0x30, 0x30, 0x30 ,0x35 ,0x31 ,0x41,0x36 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30 ,0x30    
//    				,0x30, 0x30 ,0x30 ,0x30 ,0x30 ,0x30, 0x30, 0x30 ,0x30 ,0x30 ,0x30,0x30 ,0x32 ,0x30 ,0x31 ,0x34 ,0x30 ,0x39 ,0x32 ,0x30    
//    				,0x31, 0x37 ,0x31 ,0x37 ,0x32 ,0x34, 0x54, 0x32 ,0x30 ,0x31 ,0x34,0x30 ,0x39 ,0x32 ,0x30 ,0x31 ,0x31 ,0x35 ,0x35 ,0x34    
//    				,0x36, 0x30 ,0x34 ,0x36 ,0x30 ,0x54, 0x32, 0x30 ,0x31 ,0x34 ,0x30,0x39 ,0x32 ,0x30 ,0x31 ,0x31 ,0x35 ,0x37 ,0x32 ,0x32    
//    				,0x30, 0x34 ,0x36 ,0x30 ,0x54 ,0x32, 0x30, 0x31 ,0x34 ,0x30 ,0x39,0x32 ,0x30 ,0x31 ,0x32 ,0x30 ,0x30 ,0x30 ,0x33 ,0x30    
//    				,0x34, 0x36 ,0x30 ,0x54 ,0x32 ,0x30, 0x31, 0x34 ,0x30 ,0x39 ,0x32,0x30 ,0x31 ,0x32 ,0x30 ,0x32 ,0x34 ,0x35 ,0x30 ,0x34    
//    				,0x36, 0x30 ,0x54 ,0x32 ,0x30 ,0x31, 0x34, 0x30 ,0x39 ,0x32 ,0x30,0x31 ,0x32 ,0x30 ,0x35 ,0x30 ,0x39 ,0x30 ,0x34 ,0x36    
//    				,0x30, 0x54 ,0x32 ,0x30 ,0x31 ,0x34, 0x30, 0x39 ,0x32 ,0x30 ,0x31,0x36 ,0x32 ,0x32 ,0x35 ,0x38 ,0x30 ,0x34 ,0x36 ,0x30    
//    				,0x54, 0x32 ,0x30 ,0x31 ,0x34 ,0x30, 0x39, 0x32 ,0x30 ,0x31 ,0x36,0x32 ,0x38 ,0x31 ,0x31 ,0x30 ,0x34 ,0x36 ,0x30 ,0x54    
//    				,0x32, 0x30 ,0x31 ,0x34 ,0x30 ,0x39, 0x32, 0x30 ,0x31 ,0x36 ,0x33,0x30 ,0x34 ,0x33 ,0x30 ,0x34 ,0x36 ,0x30 ,0x54 ,0x32    
//    				,0x30, 0x31 ,0x34 ,0x30 ,0x39 ,0x32, 0x30, 0x31 ,0x36 ,0x33 ,0x33,0x30 ,0x39 ,0x30 ,0x34 ,0x36 ,0x30 ,0x54 ,0x32 ,0x30    
//    				,0x31, 0x34 ,0x30 ,0x39 ,0x32 ,0x30, 0x31, 0x36 ,0x33 ,0x37 ,0x35,0x30 ,0x30 ,0x34 ,0x36 ,0x30} ;   
//    	byte[] t1= {125, 0, 0, 0, 0, 0, 0, 0};
//    	byte[]  bytes={0x20, 0x01 ,0, 0, 0, 0, 0, 0};
//    	long i=NumberFuncs.toInt(bytes);
//    	byte[] t2=NumberFuncs.toByteArray(296, 8);
//    	System.out.println(i);
    }

	
}
